﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using CommonServices;
using ContribServices;

namespace AutoClick.Algorithms
{
    /// <summary>
    /// Abstract class of AutoClick algorithms
    /// </summary>
    public abstract class AutoClickAlgorithmBase
    {
        #region Variables
        private bool _leftClickHeld = false;
        private bool _rightHeld = false;
        #endregion

        #region Click's interactions
        /// <summary>
        /// Method that launchs clicks functions of the string parameter
        /// "clickLeft" -> left click
        /// "clickRight" -> right click
        /// "clickDouble" -> double click
        /// "clickHold" -> hold the click and release it on next click
        /// </summary>
        /// <param name="kindOfClick"></param>
        public void LaunchClick(string kindOfClick, IPointerDeviceDriver pointerDeviceDriver)
        {
            switch (kindOfClick)
            {
                case "clickLeft":
                    releaseClickIfNeeded(pointerDeviceDriver);
                    pointerDeviceDriver.SimulateButtonDown(ButtonInfo.DefaultButton, "");
                    pointerDeviceDriver.SimulateButtonUp(ButtonInfo.DefaultButton, "");
                    Debug.WriteLine("Left click");
                    break;
                case "clickDoubled":
                    releaseClickIfNeeded(pointerDeviceDriver);
                    pointerDeviceDriver.SimulateButtonDown(ButtonInfo.DefaultButton, "");
                    pointerDeviceDriver.SimulateButtonUp(ButtonInfo.DefaultButton, "");
                    pointerDeviceDriver.SimulateButtonDown(ButtonInfo.DefaultButton, "");
                    pointerDeviceDriver.SimulateButtonUp(ButtonInfo.DefaultButton, "");
                    Debug.WriteLine("Double click");
                    break;
                case "clickRight":
                    releaseClickIfNeeded(pointerDeviceDriver);
                    pointerDeviceDriver.SimulateButtonDown(ButtonInfo.XButton, "Right");
                    pointerDeviceDriver.SimulateButtonUp(ButtonInfo.XButton, "Right");
                    Debug.WriteLine("Right click");
                    break;
                case "gesture":
                    releaseClickIfNeeded(pointerDeviceDriver);
                    if (_rightHeld)
                    {
                        pointerDeviceDriver.SimulateButtonUp(ButtonInfo.XButton, "Right");
                        Debug.WriteLine("Release gesture");
                        _rightHeld = false;
                    }
                    else
                    {
                        pointerDeviceDriver.SimulateButtonDown(ButtonInfo.XButton, "Right");
                        Debug.WriteLine("Hold gesture");
                        _rightHeld = true;
                    }
                    break;
                case "clickHold":
                    if (_leftClickHeld)
                    {
                        pointerDeviceDriver.SimulateButtonUp(ButtonInfo.DefaultButton, "");
                        Debug.WriteLine("Release click");
                        _leftClickHeld = false;
                    }
                    else
                    {
                        pointerDeviceDriver.SimulateButtonDown(ButtonInfo.DefaultButton, "");
                        Debug.WriteLine("Hold click");
                        _leftClickHeld = true;
                    }
                    break;
                default : break;
            }
        }
        /// <summary>
        /// Releases click if MouseButton left is already hold
        /// </summary>
        /// <param name="pointerDeviceDriver"></param>
        public void releaseClickIfNeeded(IPointerDeviceDriver pointerDeviceDriver)
        {
            if (_leftClickHeld)
            {
                pointerDeviceDriver.SimulateButtonUp(ButtonInfo.DefaultButton, "");
                Debug.WriteLine("Release click");
                _leftClickHeld = false;
            }
        }
        #endregion
    }
}
